package com.smile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cronutils.model.field.expression.On;
import com.smile.api.OssApi;
import com.smile.commons.file.FileUtils;
import com.smile.commons.gps.RouteFileUtils;
import com.smile.commons.gps.domain.*;
import com.smile.commons.gps.domain.kml.*;
import com.smile.commons.gps.kml.*;
import com.smile.commons.page.R;
import com.smile.mapper.*;
import com.smile.model.domain.*;
import com.smile.model.dto.request.FlightTaskRequestDTO;
import com.smile.model.dto.request.GenerateAirlineFileRequestDTO;
import com.smile.model.dto.request.GenerateWaypointActionRequestDTO;
import com.smile.model.dto.request.GenerateWaypointRequestDTO;
import com.smile.model.response.PutFileResponse;
import com.smile.model.response.response.GenerateAirlineFileResponseDTO;
import com.smile.service.AirportService;
import com.smile.service.FlightTaskProcessService;
import com.smile.service.FlightTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FlightTaskProcessServiceImpl implements FlightTaskProcessService {

    @Autowired
    private FlightTaskMapper flightTaskMapper;
    @Autowired
    private InspectionTaskMapper  inspectionTaskMapper;

    @Autowired
    private AirportMapper airportMapper;

    @Autowired
    AirLineMapper airLineMapper;

    @Autowired
    private AirportDroneMapper airportDroneMapper;
    @Override
    public void addTask(FlightTaskRequestDTO flightTaskRequestDTO) {
        FeisuanFlightTask feisuanFlightTask = BeanUtil.copyProperties(flightTaskRequestDTO, FeisuanFlightTask.class);
        QueryWrapper<FeisuanAirport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gateway_sn", feisuanFlightTask.getGatewaySn());
        queryWrapper.last("limit 1");
        FeisuanAirport feisuanAirport = airportMapper.selectOne(queryWrapper);
        if(ObjectUtil.isNotNull(feisuanAirport)){//航线任务
            feisuanFlightTask.setOrganizationId(feisuanAirport.getOrganizationId());
            feisuanFlightTask.setAirportId(feisuanAirport.getId());
            QueryWrapper<FeisuanAirportDrone> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("airport_id", feisuanAirport.getId());
            queryWrapper1.last("limit 1");
            FeisuanAirportDrone feisuanAirportDrone = airportDroneMapper.selectOne(queryWrapper1);
            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
                feisuanFlightTask.setDroneId(feisuanAirportDrone.getDroneId());
            }

        }
        flightTaskMapper.insert(feisuanFlightTask);
    }

    @Override
    public void updateTask(FlightTaskRequestDTO flightTaskRequestDTO) {
        FeisuanFlightTask feisuanFlightTask = BeanUtil.copyProperties(flightTaskRequestDTO, FeisuanFlightTask.class);
        QueryWrapper<FeisuanFlightTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flight_id", feisuanFlightTask.getFlightId());
        queryWrapper.last("limit 1");
        FeisuanFlightTask feisuanFlightTask1 = flightTaskMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNotNull(feisuanFlightTask1)) {
            if(ObjectUtil.isNotEmpty(flightTaskRequestDTO.getEndTime())){
                feisuanFlightTask1.setEndTime(flightTaskRequestDTO.getEndTime());
                try{
                    Duration duration =Duration.between(feisuanFlightTask1.getStartTime(),flightTaskRequestDTO.getEndTime());
                    Long t = duration.getSeconds();
                    feisuanFlightTask1.setFlightTime(t);
                }catch (Exception e){
                    System.out.println(e.getMessage());
                    System.out.println("获取时长失败");
                }

                if(feisuanFlightTask1.getTaskType() == 1){//航线任务
                    QueryWrapper<FeisuanInspectionTask> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("flight_id", feisuanFlightTask1.getFlightId());
                    queryWrapper1.last("limit 1");
                    FeisuanInspectionTask feisuanInspectionTask = inspectionTaskMapper.selectOne(queryWrapper1);
                    if(ObjectUtil.isNotNull(feisuanInspectionTask)){
                        FeisuanAirline feisuanAirline = airLineMapper.selectById(feisuanInspectionTask.getAirlineId());
                        if(ObjectUtil.isNotNull(feisuanAirline)){
                            feisuanFlightTask1.setFlightDistance(feisuanAirline.getTotalDistance());
                        }
                    }
                }
            }

            if(ObjectUtil.isNotEmpty(flightTaskRequestDTO.getBackTime())){
                feisuanFlightTask1.setBackTime(flightTaskRequestDTO.getBackTime());
            }
            if(ObjectUtil.isNotEmpty(flightTaskRequestDTO.getStatus())){
                feisuanFlightTask1.setStatus(flightTaskRequestDTO.getStatus());
            }
            flightTaskMapper.updateById(feisuanFlightTask1);
        }

    }

    @Override
    public String findTask(FlightTaskRequestDTO flightTaskRequestDTO) {
        System.out.println(flightTaskRequestDTO);

        QueryWrapper<FeisuanFlightTask> queryWrapper = new QueryWrapper<>();
        System.out.println(flightTaskRequestDTO.getFlightId());
        queryWrapper.eq("flight_id", flightTaskRequestDTO.getFlightId());
        queryWrapper.last("limit 1");
        FeisuanFlightTask feisuanFlightTask1 = flightTaskMapper.selectOne(queryWrapper);
        System.out.println(feisuanFlightTask1);
        if(feisuanFlightTask1 == null){
            return null;
        }else{
            return feisuanFlightTask1.getFlightId();
        }
    }
}
